//
//  RealNameViewController.m
//  PigMarket
//
//  Created by mac on 2019/3/22.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#pragma mark --------- 实名认证

#import "RealNameViewController.h"
#import "SystemSetingCell.h"
#import "RealNameHeaderView.h"
#import "DetailRealNameViewController.h"
#import "UploadIdCardViewController.h"

@interface RealNameViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 myTableView
 */
@property (nonatomic , strong) UITableView *myTableView;

/**
 dataArr
 */
@property (nonatomic , strong) NSArray *dataArr;
/**
 requestDataArr
 */
@property (nonatomic , strong) NSMutableArray *requestDataArr;
/**
 headerView
 */
@property (nonatomic , strong) RealNameHeaderView *headerView;
@end

@implementation RealNameViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.baseNavView.navTitleLabel.text = @"实名认证";
    [self setUpView];
}


#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.myTableView];
}

- (void) initLayout {
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(TJ_NavBarHeight);
        make.left.right.bottom.mas_equalTo(self.view);
    }];
}

- (void) initAction {
    
}

- (void)initData {
    self.dataArr = @[@{@"title":@"个人信息",@"detail":@""},
                     @{@"title":@"证件上传",@"detail":@"去绑定"}];
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    @weakify(self);
    [[[PigMarketApi sharedInstance] getRealNameMessageWithMemberId:memberId] subscribeNext:^(RealName * _Nullable realName) {
        @strongify(self);
        
        [self.requestDataArr addObject:realName];
        if ([realName.status intValue]== 1) {//通过实名认证
            self.headerView.realNameStatusLab.text = @"已实名";
            self.headerView.nameLab.text = realName.memberName;
            self.headerView.idCardNuLab.text = realName.idCard;
            self.headerView.detailLimitTimeLab.text = realName.idCardTerm;
        }else {
            self.headerView.realNameStatusLab.text = @"未实名";
            self.headerView.nameLab.text = @"姓名";
            self.headerView.idCardNuLab.text = @"-- -- ----";
            self.headerView.limitTimeLab.text = @"有效期";
            self.headerView.detailLimitTimeLab.text = @"---- -- --";
        }
    } error:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 260;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    self.headerView = [[RealNameHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 260)];
    self.headerView.backgroundColor = [UIColor globalBackgroundColor];
    return self.headerView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SystemSetingCell *cell = [SystemSetingCell cellWithTableView:tableView];
    cell.infoDic = self.dataArr[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        DetailRealNameViewController *detailVC = [[DetailRealNameViewController alloc] init];
        [self.navigationController pushViewController:detailVC animated:YES];
    }else {
        UploadIdCardViewController *detailVC = [[UploadIdCardViewController alloc] init];
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}
#pragma mark - target action

#pragma mark - private method

#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.rowHeight = 54;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}
- (NSArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSArray alloc] init];
    }
    return _dataArr;
}
- (NSMutableArray *)requestDataArr {
    if (_requestDataArr) {
        _requestDataArr = [[NSMutableArray alloc] init];
    }
    return _requestDataArr;
}
//- (RealNameHeaderView *)headerView {
//    if (_headerView) {
//        _headerView = [[RealNameHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 260)];
//        _headerView.backgroundColor = [UIColor globalBackgroundColor];
//    }
//    return _headerView;
//}
@end
